package edu.guigu.servicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import edu.guigu.servicedemo.aidl.MyAiidlService;
import edu.guigu.servicedemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnStart.setOnClickListener(this);
        binding.btnStop.setOnClickListener(this);
        binding.btnBind.setOnClickListener(this);
        binding.btnBindAction.setOnClickListener(this);
        binding.btnUnbind.setOnClickListener(this);
        binding.btnForeground.setOnClickListener(this);
        binding.btnForegroundStop.setOnClickListener(this);


//        binding.btnAidlBind.setOnClickListener(this);
//        binding.btnAidlBindAction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == binding.btnStart) {
            Intent intent = new Intent();
            intent.setClass(this, MyService1.class);
            startService(intent);
        }
        if (v == binding.btnStop) {
            Intent intent = new Intent();
            intent.setClass(this, MyService1.class);
            stopService(intent);
        }
        if (v == binding.btnBind) {
            Intent intent = new Intent();
            intent.setClass(this, MyService2.class);
            // startService(intent)
            bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        }
        if (v == binding.btnBindAction) {
            if (myBinder != null) {
                myBinder.startMusice();
            }
        }
        if (v == binding.btnUnbind) {
            if (myBinder != null) {
                unbindService(serviceConnection);
                myBinder = null;
            }
        }
        if (v == binding.btnForeground) {
            Intent intent = new Intent();
            intent.setClass(this, MyForegroundService.class);
            startForegroundService(intent);
        }
        if (v == binding.btnForegroundStop) {
            Intent intent = new Intent();
            intent.setClass(this, MyForegroundService.class);
            stopService(intent);
        }

//        if( v == binding.btnAidlBind){
//            Intent intent = new Intent();
//            intent.setPackage("edu.guigu.servicedemo");
//            intent.setAction("edu.guigu.servicedemo.ACTION_AIDL");
//            bindService(intent,aidlServiceConnection,BIND_AUTO_CREATE);
//        }
//        if(v == binding.btnAidlBindAction){
//            // 随机一个1- 3之间的值
//            int type = (int) (Math.random()*100 % 3) + 1;
//            if(iRemoteAidl != null){
//                try {
//                    iRemoteAidl.action(type);
//                } catch (RemoteException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
    }

//    private IRemoteAidl iRemoteAidl;
//    private ServiceConnection aidlServiceConnection = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            iRemoteAidl = IRemoteAidl.Stub.asInterface(service);
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    };

    MyService2.MyBinder myBinder = null;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService2.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}